package com.ax.center.service.impl;

import cn.hutool.core.util.BooleanUtil;
import com.ax.center.dto.ApiResponse;
import com.ax.center.entity.Announcement;
import com.ax.center.entity.AnnouncementReceiver;
import com.ax.center.mapper.AnnouncementMapper;
import com.ax.center.mapper.AnnouncementReceiverMapper;
import com.ax.center.service.AnnouncementReceiverService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zy
 * @date 2023/7/10 14:32
 */

@Service
public class AnnouncementReceiverServiceImpl extends ServiceImpl<AnnouncementReceiverMapper, AnnouncementReceiver> implements AnnouncementReceiverService {

    @Autowired
    private AnnouncementMapper announcementMapper;

    @Autowired
    private AnnouncementReceiverMapper announcementReceiverMapper;


    @Override
    public void saveAll(List<AnnouncementReceiver> announcementUsers) {
      saveBatch(announcementUsers);
    }

    @Override
    public ApiResponse queryByUserId(Long userId, String orderBy) {
        List<AnnouncementReceiver> list = query().eq("user_id", userId).eq("read_status", "未读").list();
        List<Long> announcementIds = list.stream().map(AnnouncementReceiver::getAnnouncementId).collect(Collectors.toList());
       if(announcementIds.isEmpty()){
           return ApiResponse.fail("没有公告");
       }
        QueryWrapper<Announcement> announcementQueryWrapper = new QueryWrapper<>();
        announcementQueryWrapper.in("id", announcementIds);

        // 根据不同的排序方式设置排序字段和排序规则
        switch (orderBy) {
            case "publishTime":
                announcementQueryWrapper.orderByDesc("publish_time");
                break;
            case "publisher":
                announcementQueryWrapper.orderByDesc("publisher");
                break;
            case "category":
                announcementQueryWrapper.orderByDesc("category");
                break;
            default:
                announcementQueryWrapper.orderByDesc("publish_time");
                break;
        }

        List<Announcement> announcements = announcementMapper.selectList(announcementQueryWrapper);
        return ApiResponse.ok(announcements);
    }


    @Override
    public ApiResponse updateReadStatus(Long userId,Long anId) {
        boolean update = update().set("read_status", "已读").eq("user_id",userId).eq("announcement_id",anId).update();
        if(BooleanUtil.isTrue(update)){
            return ApiResponse.ok();
        }else {
            return ApiResponse.fail("未知错误");
        }
    }

    @Override
    public ApiResponse deleteById(Long userId,Long anId) {
        QueryWrapper<AnnouncementReceiver> announcementQueryWrapper = new QueryWrapper<>();
        announcementQueryWrapper.eq("user_id",userId).eq("announcement_id",anId);
        announcementReceiverMapper.delete(announcementQueryWrapper);
        return ApiResponse.ok("删除成功");
    }


}
